<template>
  <div class="flex">
    <ga-link @click="operationsHandleSelect('edit')">控制台</ga-link>
    <ga-link @click="operationsHandleSelect('edit')">同步</ga-link>
    <ga-dropdown position="br" @select="operationsHandleSelect">
      <ga-link> <icon-more style="cursor: pointer" /></ga-link>
      <template #content>
        <ga-doption>开机</ga-doption>
        <ga-doption>关机</ga-doption>
        <ga-doption>重启</ga-doption>
        <ga-doption>销毁</ga-doption>
      </template>
    </ga-dropdown>
  </div>
</template>

<script setup>
import { toRefs } from "vue";
import { deleteList, sync } from "@/api/platformMgt";
import { Message } from "@garco-design/web-vue";
import { deleteModal } from "@/hooks/deleteModal";

const props = defineProps({
  record: {
    type: Object,
    required: true,
  },
});
const { record } = toRefs(props);
const emits = defineEmits(["handleEditOrAdd", "fetchData"]);
const operationsHandleSelect = async (action) => {
  if (action === "edit") {
    emits("handleEditOrAdd");
  } else if (action === "delete") {
    deleteModal(`确定要删除当前数据-[${record.value?.name}]`, async () => {
      try {
        await deleteList(record.value?.id);
        Message.success("删除成功");
        emits("fetchData");
      } catch (err) {
        console.log(err);
      }
    });
  } else if (action === "sync") {
    try {
      const { code } = await sync(record.value.id);
      if (code === 20000) Message.success("操作成功，后台同步中");
      record.value.status = "syncing";
    } catch (e) {
      console.log(e);
    }
  }
};
</script>

<style scoped lang="less"></style>
